package com.foredu.fiqs.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foredu.fiqs.domain.SalaryItemType;
import com.foredu.fiqs.service.IManagementService;
import com.foredu.fiqs.web.actionform.SalaryItemTypeActionForm;

public class SalaryItemTypeAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public IManagementService getManagementService() {
		return managementService;
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<SalaryItemType> types = managementService.findSalaryItemType();
		request.setAttribute("types", types);
		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("add");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SalaryItemTypeActionForm saf = (SalaryItemTypeActionForm) form;
		managementService.deleteSalaryItemType(saf.getId());
		List<SalaryItemType> types = managementService.findSalaryItemType();
		request.setAttribute("types", types);
		return mapping.findForward("delete");
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SalaryItemTypeActionForm saf = (SalaryItemTypeActionForm) form;
		SalaryItemType type = managementService.findSalaryItemTypeById(saf
				.getId());
		saf.setId(type.getId());
		saf.setName(type.getName());
		request.setAttribute("type", type);
		return mapping.findForward("toUpdate");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SalaryItemTypeActionForm saf = (SalaryItemTypeActionForm) form;
		SalaryItemType type = new SalaryItemType();
		type.setName(saf.getName());
		managementService.saveSalaryItemType(type);
		List<SalaryItemType> types = managementService.findSalaryItemType();
		request.setAttribute("types", types);
		return mapping.findForward("save");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SalaryItemTypeActionForm saf = (SalaryItemTypeActionForm) form;
		SalaryItemType type = managementService.findSalaryItemTypeById(saf
				.getId());
		type.setName(saf.getName());
		managementService.updateSalaryItemType(type);

		List<SalaryItemType> types = managementService.findSalaryItemType();
		request.setAttribute("types", types);

		return mapping.findForward("update");
	}
}
